package myservlet.control;
//删除信息
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mybean.data.login;

public class deleteManager extends HttpServlet{
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
			super.init(config);
			try {//加载驱动
				Class.forName("com.mysql.jdbc.Driver");	
			}
			catch(Exception e) {}
		}
	public  void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		String form= req.getParameter("type");//要执行删除操作的表单
		String id= req.getParameter("number");//获取需要删除的编号
		String condition="";
		if(form.equals("manager"))
			condition="delete from manager where MID='"+id+"'";//删除单车管理人信息
		else if(form.equals("fixshop"))
			condition="delete from fixshop where Fno='"+id+"'";//删除维修厂信息
		else if(form.equals("fix"))
			condition="delete from fix where B_id='"+id+"'";//删除维修记录
		else if(form.equals("district"))
			condition="delete from district where Dno='"+id+"'";//删除地区信息
		login loginBean =null; 
		HttpSession session= req.getSession(true);
		Connection con = null;
		try {
			loginBean = (login)session.getAttribute("loginBean");
			boolean b=loginBean.getLogname()==null || loginBean.getLogname().length()==0;
			if(b){
				out.println("<script language = javascript>alert('请重新登录!');");
				out.println("location.href='index.jsp'</script>");
				return;
			}
			String url = "jdbc:mysql://127.0.0.1:3306/bike?&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true";
			PreparedStatement sql;
			try {//连接数据库
				con = DriverManager.getConnection(url,"root","744615");
				sql=con.prepareStatement(condition); 
				sql.executeUpdate();                  
				sql.close(); 
				out.println("<script language = javascript>alert('删除成功!');");
				out.println("location.href='Ahomepage.jsp'</script>");
			}
			catch (SQLException exp){
				System.out.println(exp);
				out.println("<script language = javascript>alert('更新数据库失败，删除失败!');");
				out.println("location.href='Ahomepage.jsp'</script>");
			}
			finally{   
	            // 关闭Connection  
	            con.close();   
			}	
		}
		catch (Exception exp){
			System.out.println(exp);
			out.println("<script language = javascript>alert('更新数据库失败，删除失败!');");
			out.println("location.href='Ahomepage.jsp'</script>");			
		}
	}	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
}
